/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.github.ngocnh210.TrueName.Util;

import java.util.regex.Matcher;
import org.bukkit.entity.Player;

/**
 *
 * @author NgocNH
 */
public class Util {
    public static boolean checkFilter(String action, String string) {
        if (action.equalsIgnoreCase("username")) {
            int lengtha = string.length();
            int lengthb = Config.filter_username.length();
            int i = 0;
            char thechar1, thechar2;
            while (i < lengtha) {
                thechar1 = string.charAt(i);
                int a = 0;
                while (a < lengthb) {
                    thechar2 = Config.filter_username.charAt(a);
                    if (thechar1 == thechar2 || thechar1 == '\'' || thechar1 == '\"') {
                        Config.has_badcharacters = true;
                        return false;
                    }
                    a++;
                }
                i++;
            }
            Config.has_badcharacters = false;
            return true;
        }
        return true;
    }

    public static String replaceStrings(String string, Player player, String additional) {
        String extra = "";
        if (additional != null) {
            extra = additional;
        }
        if (!Config.has_badcharacters && player != null && player.getName().length() > Integer.parseInt(Config.username_minimum_length) && player.getName().length() < Integer.parseInt(Config.username_maximum_length)) {
            string = string.replaceAll("\\{PLAYER\\}", player.getName());
            string = string.replaceAll("&", "§");
        } else {
            string = string.replaceAll("&", Matcher.quoteReplacement("�"));
        }

        // Replacement variables
        string = string.replaceAll("\\{USERMINLENGTH\\}", Config.username_minimum_length);
        string = string.replaceAll("\\{USERMAXLENGTH\\}", Config.username_maximum_length);
        string = string.replaceAll("\\{VERSION\\}", Config.version);
        string = string.replaceAll("\\{USERBADCHARACTERS\\}", Matcher.quoteReplacement(Config.filter_check));
        string = string.replaceAll("\\{NEWLINE\\}", System.getProperty("line.separator"));
        string = string.replaceAll("\\{newline\\}", System.getProperty("line.separator"));
        string = string.replaceAll("\\{N\\}", System.getProperty("line.separator"));
        string = string.replaceAll("\\{n\\}", System.getProperty("line.separator"));
        string = string.replaceAll("\\{NL\\}", System.getProperty("line.separator"));
        string = string.replaceAll("\\{nl\\}", System.getProperty("line.separator"));

        // Uppercase colors
        string = string.replaceAll("\\{BLACK\\}", "§0");
        string = string.replaceAll("\\{DARKBLUE\\}", "§1");
        string = string.replaceAll("\\{DARKGREEN\\}", "§2");
        string = string.replaceAll("\\{DARKTEAL\\}", "§3");
        string = string.replaceAll("\\{DARKRED\\}", "§4");
        string = string.replaceAll("\\{PURPLE\\}", "§5");
        string = string.replaceAll("\\{GOLD\\}", "§6");
        string = string.replaceAll("\\{GRAY\\}", "§7");
        string = string.replaceAll("\\{DARKGRAY\\}", "§8");
        string = string.replaceAll("\\{BLUE\\}", "§9");
        string = string.replaceAll("\\{BRIGHTGREEN\\}", "§a");
        string = string.replaceAll("\\{TEAL\\}", "§b");
        string = string.replaceAll("\\{RED\\}", "§c");
        string = string.replaceAll("\\{PINK\\}", "§d");
        string = string.replaceAll("\\{YELLOW\\}", "§e");
        string = string.replaceAll("\\{WHITE\\}", "§f");

        string = string.replaceAll("\\{BLACK\\}", "§0");
        string = string.replaceAll("\\{NAVY\\}", "§1");
        string = string.replaceAll("\\{GREEN\\}", "§2");
        string = string.replaceAll("\\{BLUE\\}", "§3");
        string = string.replaceAll("\\{RED\\}", "§4");
        string = string.replaceAll("\\{PURPLE\\}", "§5");
        string = string.replaceAll("\\{GOLD\\}", "§6");
        string = string.replaceAll("\\{LIGHTGRAY\\}", "§7");
        string = string.replaceAll("\\{GRAY\\}", "§8");
        string = string.replaceAll("\\{DARKPURPLE\\}", "§9");
        string = string.replaceAll("\\{LIGHTGREEN\\}", "§a");
        string = string.replaceAll("\\{LIGHTBLUE\\}", "§b");
        string = string.replaceAll("\\{ROSE\\}", "§c");
        string = string.replaceAll("\\{LIGHTPURPLE\\}", "§d");
        string = string.replaceAll("\\{YELLOW\\}", "§e");
        string = string.replaceAll("\\{WHITE\\}", "§f");

        // Lowercase colors
        string = string.replaceAll("\\{black\\}", "§0");
        string = string.replaceAll("\\{darkblue\\}", "§1");
        string = string.replaceAll("\\{darkgreen\\}", "§2");
        string = string.replaceAll("\\{darkteal\\}", "§3");
        string = string.replaceAll("\\{darkred\\}", "§4");
        string = string.replaceAll("\\{purple\\}", "§5");
        string = string.replaceAll("\\{gold\\}", "§6");
        string = string.replaceAll("\\{gray\\}", "§7");
        string = string.replaceAll("\\{darkgray\\}", "§8");
        string = string.replaceAll("\\{blue\\}", "§9");
        string = string.replaceAll("\\{brightgreen\\}", "§a");
        string = string.replaceAll("\\{teal\\}", "§b");
        string = string.replaceAll("\\{red\\}", "§c");
        string = string.replaceAll("\\{pink\\}", "§d");
        string = string.replaceAll("\\{yellow\\}", "§e");
        string = string.replaceAll("\\{white\\}", "§f");

        string = string.replaceAll("\\{black\\}", "§0");
        string = string.replaceAll("\\{navy\\}", "§1");
        string = string.replaceAll("\\{green\\}", "§2");
        string = string.replaceAll("\\{blue\\}", "§3");
        string = string.replaceAll("\\{red\\}", "§4");
        string = string.replaceAll("\\{purple\\}", "§5");
        string = string.replaceAll("\\{gold\\}", "§6");
        string = string.replaceAll("\\{lightgray\\}", "§7");
        string = string.replaceAll("\\{gray\\}", "§8");
        string = string.replaceAll("\\{darkpurple\\}", "§9");
        string = string.replaceAll("\\{lightgreen\\}", "§a");
        string = string.replaceAll("\\{lightblue\\}", "§b");
        string = string.replaceAll("\\{rose\\}", "§c");
        string = string.replaceAll("\\{lightpurple\\}", "§d");
        string = string.replaceAll("\\{yellow\\}", "§e");
        string = string.replaceAll("\\{white\\}", "§f");

        return string;
    }
}
